<?php
require_once ('Zwei/Db/TableLoggeable.php');
class UsuariosSistemaModel extends Zwei_Db_TableLoggeable
{
	protected $_name = 'usuarios_sistema';
	protected $_name_perfil = 'perfil';
	protected $_primary = "usuario";
	protected $_data_perfil = array();
	
	public function select()
	{
		$this->setAdapter("dn");
		$select = new Zend_Db_Table_Select($this);
		$select->setIntegrityCheck(false);
		
		$select
			->from($this->_name, array('usuario', 'id_perfil', 'modulo'))
			->joinLeft($this->_name_perfil, "$this->_name.id_perfil=$this->_name_perfil.id_perfil",
				array(
				    'elimina' => new Zend_Db_Expr("IF(flag_eliminar='T','Si','No')"),
				    'compra' => new Zend_Db_Expr("IF(flag_comprar='T','Si','No')"),
				    'consulta' => new Zend_Db_Expr("IF(flag_consultar='T','Si','No')"),
				    'cobro' => new Zend_Db_Expr("IF(flag_cobro_cs='T','Si','No')"),
				    'sms' => new Zend_Db_Expr("IF(flag_sms='T','Si','No')") 
				)
			)
			->order("$this->_name.modulo")
			->order("$this->_name.usuario");
		//SELECT A.usuario, A.id_perfil, A.modulo, if(B.flag_eliminar='T','Si','No') AS elimina, if(B.flag_comprar='T','Si','No') AS compra, if(B.flag_consultar='T','Si','No') AS consulta, if(B.flag_cobro_cs='T','Si','No') AS cobro, if(B.flag_sms='T','Si','No') AS sms FROM usuarios_sistema A, perfil B where A.id_perfil=B.id_perfil ORDER BY A.modulo ASC, A.usuario ASC"
		return $select;
	}
	
	
	public function update($data, $where) 
	{
		$this->setAdapter("an");
		return parent::update($data, $where);
	}
	
	public function insert($data) 
	{
        $this->setAdapter("an");		
		return parent::insert($data);
	}
	
	public function delete($where) {
		$this->setAdapter("an");
		return parent::delete($where);
	}
	
}
